package com.framework.modules.job.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.flow.FlowAccount;
import com.framework.entity.flow.FlowFrozenTerminal;
import com.framework.entity.sys.SysAgent;
import com.framework.service.AgentInfoService;
import com.framework.service.AgentService;
import com.framework.service.FlowAccountService;
import com.framework.service.FlowFrozenTerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author wq
 * @date 2019/9/17 14:04
 * @description
 */
@Component("tlZhiTuiTask")
public class TlZhiTuiTask {

    @Autowired
    private AgentService agentService;


    @Autowired
    private FlowAccountService flowAccountService;

    @Autowired
    private AgentInfoService agentInfoService;

    @Autowired
    private FlowFrozenTerminalService flowFrozenTerminalService;


    public void handleAgent(Long agentId){
        //处理agentId的直推奖励
        List<SysAgent> agents = agentService.selectList(new EntityWrapper<SysAgent>().eq("pid", agentId));

        FlowFrozenTerminal terminal = flowFrozenTerminalService.queryByAgentId(agentId);

        if(terminal==null){
            return;
        }

        for (SysAgent agent : agents) {
            FlowFrozenTerminal flowFrozenTerminal = flowFrozenTerminalService.queryByAgentId(agent.getAgentId());

            if(flowFrozenTerminal!=null){
                FlowAccount account = flowAccountService.selectOne(new EntityWrapper<FlowAccount>().eq("bel_id", flowFrozenTerminal.getSn())
                        .eq("account_type", 11)
                );

                if(account==null){
                    BigDecimal recAmount = new BigDecimal("100");
                    agentInfoService.addAmount(agent.getPid(), recAmount);
                    FlowAccount flowAccount = new FlowAccount();
                    flowAccount.setAmount(recAmount);
                    flowAccount.setAccountType(11);
                    flowAccount.setSupportType(2);
                    flowAccount.setBelId(flowFrozenTerminal.getSn());
                    flowAccount.setCreateTime(new Date());
                    flowAccount.setAgentId(agent.getPid());
                    flowAccount.setRemark("推荐" + agent.getName() + "奖励");
                    flowAccountService.insert(flowAccount);
                }
            }
        }
    }






}
